<?php
/**
 * IndexController
 *
 * PHP Version 5.4
 *
 * @author liuxuTJ <liuxuphp@icesmart.cn>
 */
namespace Admin\Controller;

use Admin\Service\BaseService;
use Admin\Service\PermissionService;
use Admin\Service\UserService;
use Model\SubjectsModel;
use Model\TeacherModel;
use Model\TeachingModel;
use Think\Controller;

/**
 * Class TeacherController
 */
class TeacherController extends BaseController
{

    public function __construct()
    {
        parent::__construct();
        $userService = new UserService();
        $userInfo = $userService->validateLogin();   //验证是否登录

        $permissionService = new PermissionService();


        $permission = $permissionService->validate($userInfo['userId'], [
            'addTeacher' => '/' . CONTROLLER_NAME . '/addTeacher',
            'delTeacher' => '/' . CONTROLLER_NAME . '/deleteTeacher',
            'editTeacher' => '/' . CONTROLLER_NAME . '/editTeacher',
        ]);
        $menu = $permissionService->menu($userInfo['userId']);
        $this->assign('permission', $permission);
        $this->assign('menu', $menu);
        $this->assign('userInfo', $userInfo);
    }

    /**
     * 教师列表
     */
    public function index()
    {
        $model = new TeacherModel();
        $page = $this->page($model, null);
        $show = $page->show();
        $limit[] = $page->firstRow;
        $limit[] = $page->listRows;
        $list = $model->findByFilter(null, ['id' => 'DESC'], $limit);
        $teachingModel = new TeachingModel();
        $teachingList = $teachingModel->select();
        $subjectsModel = new SubjectsModel();
        $subjectsList = $subjectsModel->select();
        $this->assign('list', $list);
        $this->assign("page", $show);
        $this->assign("subjectsList", $subjectsList);
        $this->assign("teachingList", $teachingList);
        $this->display();
    }

    /**
     * 添加教师
     */
    public function addTeacher()
    {
        $teacherModel = new TeacherModel();
        $image = $_FILES['pic'];
        $baseService = new BaseService();
        $image = $baseService->upload($image);
        $data = [
            'name' => I("post.name"),
            'level' => I("post.level"),
            'school' => I("post.school"),
            'teachingId' => I("post.teachingId"),
            'subjectsId' => I("post.subjectsId"),
            'content' => I("post.content"),
            'pic' => $image,
            'create_time' => date('Y-m-d h:i:s')
        ];
        $saveData = $teacherModel->add($data);
        if ($saveData) {
            $this->success("添加成功");
        } else {
            $this->error("添加失败");
        }
    }

    /**
     * 删除教师
     *
     * @param int $id 教师id
     *
     */

    public function delTeacher()
    {
        $id = intval(I("get.id"));
        if (empty($id) || $id == 0) {
            $this->error("非法参数！", '', 1);
        }
        $teacherModel = new TeacherModel();
        $delete = $teacherModel->delete($id);
        if ($delete) {
            $this->success('删除成功', '/Admin/Teacher', 1);
        } else {
            $this->error('删除失败', '', 1);
        }
    }

    /**
     * 修改模板
     *
     * @param int $id 教师id
     */
    public function editTeacher()
    {
        $id = intval(I("get.id"));
        if (empty($id) || $id == 0) {
            $this->error("非法参数！", '', 1);
        }
        $teacherModel = new TeacherModel();
        $data = $teacherModel->find($id);
        $teachingModel = new TeachingModel();
        $teachingList = $teachingModel->select();
        $subjectsModel = new SubjectsModel();
        $subjectsList = $subjectsModel->select();
        $this->assign("subjectsList", $subjectsList);
        $this->assign("teachingList", $teachingList);
        $this->assign("data", $data);
        $this->display();
    }

    /**
     * 修改教师处理
     *
     * @param int $id 教师id
     */
    public function modifyTeacher()
    {
        $teacherModel = new TeacherModel();
        $id = intval(I('post.id'));
        $image = $_FILES['pic'];
        if (empty($id)) {
            $this->error("非法处理参数");
        }
        $data = [
            'id' => $id,
            'name' => I("post.name"),
            'level' => I("post.level"),
            'school' => I("post.school"),
            'teachingId' => I("post.teachingId"),
            'subjectsId' => I("post.subjectsId"),
            'content' => I("post.content"),
            'create_time' => date('Y-m-d h:i:s')
        ];


        if (!empty($image['name'])) {
            $baseService = new BaseService();
            $image = $baseService->upload($image);
            $data['pic'] = $image;
        }

        $modifyData = $teacherModel->save($data);
        if ($modifyData) {
            $this->success("修改成功");
        } else {
            $this->error("修改失败");
        }
    }

}